package top.xana.acg.anime.ui.view

import android.content.Context
import android.graphics.Canvas
import android.util.AttributeSet
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView
import com.youth.banner.Banner
import com.youth.banner.adapter.BannerAdapter
import top.xana.acg.anime.R
import top.xana.acg.anime.utils.radius
import kotlin.math.abs

/**
 * Created by xana on 2022/3/23 0023
 * Describe: cv4096@qq.com
 */
class SelfBannerView<T, BA : BannerAdapter<T, RecyclerView.ViewHolder>> @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : Banner<T, BA>(context, attrs, defStyleAttr) {

    private var startX = 0f
    private var startY = 0f
    override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
        val f = super.dispatchTouchEvent(ev)
        when (ev.action) {
            MotionEvent.ACTION_DOWN -> {
                parent.requestDisallowInterceptTouchEvent(true)
                startX = ev.x
                startY = ev.y
            }
            MotionEvent.ACTION_MOVE -> {
                val dx = ev.x - startX
                val dy = ev.y - startY
                if (abs(dy) > abs(dx)) {
                    parent.requestDisallowInterceptTouchEvent(false)
                } else {
                    parent.requestDisallowInterceptTouchEvent(true)
                }
            }
            else -> parent.requestDisallowInterceptTouchEvent(false)
        }
        return f
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        radius(R.dimen.dp_8)
    }

}